using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using sCicero;

namespace sCicero
{
    public partial class student_new : UserControl
    {
        private int m_nClassid;
        private sCicero.scicero m_osCicero;

        public student_new(ref sCicero.scicero osCicero)
        {
            m_osCicero = osCicero;
            InitializeComponent();
            comboBox_classes.DataSource = m_osCicero.Classes.GetDefaultView();//DataTable();
            comboBox_classes.DisplayMember = "name";
            comboBox_classes.ValueMember = "id";

        }

        public int ClassId
        {
            get
            {
                return m_nClassid;
            }
            set
            {
                m_nClassid = value;
                comboBox_classes.SelectedValue = m_nClassid;
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox_firstname.Text.Trim() != "" && textBox_lastname.Text.Trim() != "")
            {
                if (m_osCicero.Students.New(textBox_firstname.Text.Trim(), textBox_lastname.Text.Trim(), m_nClassid) == sCicero.Errorcode.NOERROR)
                {
                    m_osCicero.Event.Raise_StudentCreated(this, textBox_firstname.Text.Trim() + ' ' + textBox_lastname.Text.Trim(), m_osCicero.Classes.Get(m_nClassid)["name"].ToString());
                    m_osCicero.Event.Raise_UpdateRequest(this, "Ny elev, " + textBox_firstname.Text.Trim() + ' ' + textBox_lastname.Text.Trim() + ", sparad", false);
                    textBox_firstname.Text = "";
                    textBox_lastname.Text = "";
                }
            }
            else
                MessageBox.Show("Fyll i ett namn!");
        }

        private void comboBox_classes_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                m_nClassid = int.Parse(comboBox_classes.SelectedValue.ToString());
            }
            catch
            {}
        }

        private void student_new_Load(object sender, EventArgs e)
        {
            textBox_firstname.Focus();
        }

    }
}
